// pages/college_count/college_count.js
var mm = require("/../js/thenBy.js");   //调用第三方库
Page({

  /**
   * 页面的初始数据
   */
  data: {
    things_school_id: '16618',
    listArr: [],        //循环中班级请假信息界面所需数据暂时存放变量
    Arr: [],            //班级请假信息界面所需数据
    h: [],
    a: 0,               //第二次分组时，循环所需的变量
    c: 0,               //向listArr数组插入信息时，循环所需的变量
    d: 0,               //向listArr数组插入信息的循环中，maybe的下标
    sum: 0,             //listArr数组的对象个数
    con: 0,             //0常量
  },

  GroupBy(array, fn){      //groupby分组函数
    const groups ={};
    array.forEach(
      function(item){
        const group = JSON.stringify(fn(item));
        groups[group] = groups[group] || [];
        groups[group].push(item);
      }
    );
    return Object.keys(groups).map(
      function(group){
        return groups[group];
      }
    );
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({   //获取上个页面传过来的input_data
      things_school_id: options.things_school_id
    })
    this.getLeave()
  },

  getLeave(){
    var that = this
    wx.cloud.callFunction({      //调用云函数
      name: 'find',
      data: {
        name_collection:"overdue_table",//集合名称
        condition:{//条件
          college_examine: true,
          teacher_examine: true,
          things_school_id: that.data.things_school_id
        },
        data:{
        }
      },
      success(res){
        //console.log("success",res);     //打印成功回调
        that.setData({
          h: res.result.data
        })
        var m = 0;
        that.data.h.forEach(item => {item.input_classA = ''});
        that.data.h.forEach(item => {item.input_classB = ''});
        that.data.h.forEach(item => {item.input_classC = ''});
        that.data.h.forEach(item => {item.input_classD = ''});
        while(m < that.data.h.length){
          var input_classA = 'h['+m+'].input_classA';
          var input_classB = 'h['+m+'].input_classB';
          var input_classC = 'h['+m+'].input_classC';
          var input_classD = 'h['+m+'].input_classD';
          var A = ((that.data.h[m]).input_class).split(" ");
          that.setData({
            [input_classA]: A[0],
            [input_classB]: A[1],
            [input_classC]: A[2],
            [input_classD]: A[1] + A[2]
          })
          m = m + 1;
        }
        var z = that.data.h;
        z = z.sort(mm.firstBy(function (a, b){     //年级排序  mm.firstBy().thenBy().thenBy() 调用了第三方js库：thenby.js库
          var value1 = a.input_classA;                          //调用库的语句在第二行
          var value2 = b.input_classA;
          return value1 - value2;
        }).thenBy(function (x, y){     //班级排序
          var value3 = x.input_classB;
          var value4 = y.input_classB;
          return value3.localeCompare(value4);
        }).thenBy(function (m, n){     //班级排序
          var value5 = m.input_classC.substr(0,1);
          var value6 = n.input_classC.substr(0,1);
          return value5 - value6;
        }));
        //results是根据input_classA分组后的数组
        const results = that.GroupBy(z, function(item){return [item.input_classA];});
        //console.log(results)
        var ggg = []
        while(that.data.a < results.length){
          //maybe是在results的基础上再根据stu_class分组
          const maybe = that.GroupBy(results[that.data.a], function(item){return [item.input_classD];});
          //console.log(maybe)
          var aaa = that.data.sum
          that.setData({
            sum: aaa + maybe.length   //更新listArr数组的最大长度，方便向listArr数组插入时使用循环
          })
          
          while(that.data.c < that.data.sum){            //使用循环将input_classA，input_classD，length信息插入到listArr数组
            ggg[that.data.c] = {
              input_classA: (maybe[that.data.d][0]).input_classA,   
              input_classD: (maybe[that.data.d][0]).input_classD,
              length: (maybe[that.data.d]).length
            }
            
            that.setData({
              c : that.data.c + 1,
              con : that.data.con + 1,
              d : that.data.d + 1
            })
          }
          that.setData({
            a : that.data.a + 1,
            d : 0,
            con : 0,
          })
        }
        that.setData({
          listArr:ggg
        })
        that.setData({
          //Arr: that.data.listArr,
          a: 0,
          c: 0,
          sum: 0
        })
        console.log(that.data.listArr)
      },//成功的回调
      fail(res){
        console.log("fail",res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})